import KZJRequest from '../index'
import { TLoginResult, TPhone } from './type'
import { TRequestDefaultResult } from '../type'

enum LoginApi {
  PhoneLogin = '/login/cellphone', // 手机号密码登录
  SentCode = '/captcha/sent', // 发送验证码
  VerifyCode = '/captcha/verify', //验证验证码
  Logout = '/logout', // 退出登录
  Status = '/login/status', // 刷新登录状态
  SongsList = 'user/playlist' // 获取用户创建歌单
}

export function phoneLogin(phone: TPhone) {
  return KZJRequest.post<TLoginResult>({
    url: LoginApi.PhoneLogin,
    data: {
      phone: phone.cellphone,
      password: phone.password ?? null,
      captcha: phone.code ?? null
    },
    showLoading: true
  })
}

export function phoneSentCode(phone: number | null) {
  return KZJRequest.get<TRequestDefaultResult>({
    url: LoginApi.SentCode,
    params: { phone }
  })
}

export function getUserPlayList(uid: number) {
  return KZJRequest.get<TRequestDefaultResult>({
    url: LoginApi.SongsList,
    params: { uid }
  })
}

export function phoneVerifyCode(phone: number | null, captcha: number | null) {
  return KZJRequest.post<TRequestDefaultResult>({
    url: LoginApi.VerifyCode,
    data: { phone, captcha }
  })
}

export function phoneLogout() {
  return KZJRequest.get<TRequestDefaultResult>({
    url: LoginApi.Logout
  })
}

export function loginStatus() {
  return KZJRequest.get<TRequestDefaultResult>({
    url: LoginApi.Status
  })
}
